package com.prs.crm.action.purchase;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.action.FlowAction;
import com.prs.crm.annotation.FlowActive;
import com.prs.crm.annotation.FlowStart;
import com.prs.crm.context.AppContext;
import com.prs.crm.context.Constrains;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.purchase.Inquiry;
import com.prs.crm.domain.purchase.PurchaseOrder;
import com.prs.crm.domain.purchase.SupplierInquiry;
import com.prs.crm.domain.stock.Purchase;
import com.prs.crm.domain.stock.PurchaseDetail;
import com.prs.crm.domain.stock.PurchaseType;
import com.prs.crm.domain.stock.Supplier;
import com.prs.crm.service.purchase.InquiryService;
import com.prs.crm.service.purchase.PurchaseOrderService;
import com.prs.crm.service.purchase.SupplierInquiryService;
import com.prs.crm.service.stock.PurchaseDetailService;
import com.prs.crm.service.stock.PurchaseService;
import com.prs.crm.service.stock.PurchaseTypeService;
import com.prs.crm.service.stock.SupplierService;

public class SupplierInquiryAction extends FlowAction<SupplierInquiry> {

	private static final long serialVersionUID = -106786952771704667L;
	private PurchaseService purchaseService;
	private SupplierInquiryService supplierInquiryService;
	private PurchaseTypeService purchaseTypeService;
	private PurchaseOrder purchaseOrder;
	private PurchaseOrderService purchaseOrderService;
	private InquiryService inquiryService;
	private SupplierService supplierService;
	private Purchase purchase;
	private Collection<PurchaseDetail> slist;
	private Collection<Supplier> suppliers;
	private Inquiry inquiry;
	private List<Inquiry> inquirys;
	private List<PurchaseType> purchaseTypeList;
	private String joiners;
	private String productId;
	private String stockNum;
	private Supplier supplier;
	private Integer[] ids;
	private PurchaseDetail purchaseDetail;
	private SupplierInquiry supplierInquiry;
	private PurchaseDetail[] purchasedetail;
	private Double[] nPrice;
	private Double[] nDiscount;
	private Double[] nTotalPrice;

	// 处理操作
	private PurchaseDetail[] supplierids;
	private Inquiry[] supplierifors;
	private String[] checks;
	private String[] checkss;

	private PaginationSupport pagination;
	private PurchaseDetailService purchaseDetailService;
	private String flow;

	@Action("listSupplierPurchase")
	public String listSupplierPurchase() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Purchase.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		criteria.add(Restrictions.eq("flowstatus", 2));
		this.setPagination(this.getPurchaseService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@FlowActive(target = "supplierInquiry", flowservice = "supplierInquiryService")
	@Action("editSupplierInquiry")
	public String editSupplierInquiry() {
		purchase = this.getPurchaseService().get(purchase.getId());
		this.slist = purchase.getDetails();
		Iterator<Person> iter = purchase.getJoiners().iterator();
		joiners = "";
		while (iter.hasNext()) {
			joiners += iter.next().getResume().getName() + " , ";
		}
		return SUCCESS;
	}

	@FlowStart(target = "supplierInquiry", flowservice = "supplierInquiryService")
	@Action(value = "saveSupplierInquiry", results = {
			@Result(name = "success", type = "redirect", location = "listSupplierPurchase"),
			@Result(name = "error", type = "chain", location = "editSupplierInquiry") })
	public String saveSupplierInquiry() {
		if(flow==null||flow.equals("")){
			this.addActionError("请添加添加审核流程!");
			return ERROR;
		}
		if (inquirys == null || inquirys.size() == 0) {
			this.addActionError("请添加供应商询价!");
			return ERROR;
		}

		for (Inquiry inquiry : inquirys) {
			inquiry.setSupplierInquiry(supplierInquiry);
			supplierInquiry.addInquiry(inquiry);
		}
		this.getSupplierInquiryService().save(supplierInquiry);
		Purchase PurchaseObject = this.getPurchaseService().getPurchase(
				purchase.getId());
		PurchaseObject.setIquiryTag(Constrains.INQUIRY_STATE);
		this.getPurchaseService().saveOrUpdate(PurchaseObject);
		return SUCCESS;
	}

	@Action(value = "savePurchaseOrderProcess", results = {
			@Result(name = "success", type = "redirect", location = "listSupplierInquiryProcess"),
			@Result(name = "error", type = "chain", location = "listSupplierInquiryProcess") })
	public String savePurchaseOrderProcess() {
		if (checkss.length == supplierids.length) {
			SupplierInquiry SupplierInquiryObject = this
					.getSupplierInquiryService().getSupplierInquiry(
							supplierInquiry.getId());

			SupplierInquiryObject.setProcessTag(2);
			List<String> suppliersList = new ArrayList<String>();

			for (int i = 0; i < checkss.length; i++) {
				// PurchaseDetail PurchaseDetailObject =
				// this.getPurchaseService()
				// .getDetail(supplierids[i].getId());
				// String Is[] = checkss[i].split("/");
				// supplier.setId(Integer.parseInt(Is[1]));
				// PurchaseDetailObject.setSupplier(supplier);
				// this.getPurchaseService().saveOrUpdate(PurchaseDetailObject);
				//
				// Inquiry InquiryObject = this.getInquiryService().getInquiry(
				// Integer.parseInt(Is[0]));
				// InquiryObject.setProcessTag(2);
				// this.getInquiryService().saveOrUpdate(InquiryObject);
				boolean a = true;
				for (String string : suppliersList) {
					if (string.equals(checkss[i].split("/")[1])) {
						a = false;
					}
				}
				if (a == true) {
					suppliersList.add(checkss[i].split("/")[1]);

				}
			}
			for (int i = 0; i < suppliersList.size(); i++) {
				if (i == 0) {
					SupplierInquiryObject.setProcessTag(2);
					SupplierInquiryObject.setSupplier(supplierService
							.get(Integer.parseInt(suppliersList.get(0))));
					this.getSupplierInquiryService().saveOrUapedate(
							SupplierInquiryObject);
					for (int c = 0; c < checkss.length; c++) {

						String Is[] = checkss[c].split("/");
						// supplier.setId(Integer.parseInt(Is[1]));
						// supplierids[c].setSupplier(supplierService.get(Integer.parseInt(suppliersList.get(i))));
						Inquiry InquiryObject = this.getInquiryService().get(
								Integer.parseInt(Is[0]));
						if (SupplierInquiryObject.getSupplier() == InquiryObject
								.getSupplier()) {
							supplierids[c].setSupplier(SupplierInquiryObject
									.getSupplier());
							this.purchaseDetailService.save(supplierids[c]);
							// System.out.println(InquiryObject.getId()+"    d       "+checkss[c]+" "+suppliersList.get(i)+"  "+SupplierInquiryObject.getSupplier().getName());
							// InquiryObject.setSupplier(supplierService.get(Integer.parseInt(suppliersList.get(i))));
							InquiryObject
									.setSupplierInquiry(SupplierInquiryObject);
							InquiryObject.setProcessTag(2);
							this.getInquiryService().save(InquiryObject);
						}

					}

				} else {

					SupplierInquiry NewSupplierInquiry = new SupplierInquiry();
					NewSupplierInquiry.setSign(SupplierInquiryObject.getSign());
					NewSupplierInquiry.setFlowstatus(SupplierInquiryObject
							.getFlowstatus());
					NewSupplierInquiry.setRemark(SupplierInquiryObject
							.getRemark());
					NewSupplierInquiry.setRecorder(SupplierInquiryObject
							.getRecorder());
					NewSupplierInquiry.setProcess(SupplierInquiryObject
							.getProcess());
					NewSupplierInquiry.setPurchase(SupplierInquiryObject
							.getPurchase());
					NewSupplierInquiry.setProcessTag(2);
					NewSupplierInquiry.setOrderTag(0);
					NewSupplierInquiry.setSupplier(supplierService.get(Integer
							.parseInt(suppliersList.get(i))));
					this.getSupplierInquiryService().save(NewSupplierInquiry);
					for (int c = 0; c < checkss.length; c++) {
						// PurchaseDetail PurchaseDetailObject = supplierids[c];

						String Is[] = checkss[c].split("/");
						// supplier.setId(Integer.parseInt(Is[1]));
						// supplierids[c].setSupplier(NewSupplierInquiry.getSupplier());
						Inquiry InquiryObject = this.getInquiryService()
								.getInquiry(Integer.parseInt(Is[0]));
						if (NewSupplierInquiry.getSupplier() == InquiryObject
								.getSupplier()) {
							// System.out.println(InquiryObject.getId()+"             v        "+checkss[c]+" "+suppliersList.get(i)+"  "+NewSupplierInquiry.getSupplier().getName());
							supplierids[c].setSupplier(NewSupplierInquiry
									.getSupplier());
							this.getPurchaseDetailService()
									.save(supplierids[c]);
							// InquiryObject.setSupplier(supplierService.get(Integer.parseInt(suppliersList.get(i))));
							InquiryObject
									.setSupplierInquiry(NewSupplierInquiry);
							InquiryObject.setProcessTag(2);
							this.getInquiryService().save(InquiryObject);
						}

					}
					// Inquiry InquiryObject = this.getInquiryService()
					// .getInquiry(Integer.parseInt(Is[0]));
					// InquiryObject.setProcessTag(2);
					// InquiryObject.setSupplierInquiry(NewSupplierInquiry);
					// this.getInquiryService().saveOrUpdate(InquiryObject);
				}
			}

			//			
			// for (int i = 0; i < checkss.length; i++) {
			// PurchaseDetail PurchaseDetailObject = this.getPurchaseService()
			// .getDetail(supplierids[i].getId());
			// String Is[] = checkss[i].split("/");
			// supplier.setId(Integer.parseInt(Is[1]));
			// PurchaseDetailObject.setSupplier(supplier);
			// this.getPurchaseService().saveOrUpdate(PurchaseDetailObject);
			//
			// Inquiry InquiryObject = this.getInquiryService().getInquiry(
			// Integer.parseInt(Is[0]));
			// InquiryObject.setProcessTag(2);
			// this.getInquiryService().saveOrUpdate(InquiryObject);
			//				
			// }

			// int judgeTag = 0;
			// int supplierJudgeTag = 0;
			// for (int i = 0; i < supplierids.length; i++) {
			// PurchaseDetail PurchaseDetailObject = this.getPurchaseService()
			// .getDetail(supplierids[i].getId());
			// String Is[] = checkss[i].split("/");
			// supplier.setId(Integer.parseInt(Is[1]));
			// PurchaseDetailObject.setSupplier(supplier);
			// this.getPurchaseService().saveOrUpdate(PurchaseDetailObject);
			// if (judgeTag > 0 && supplierJudgeTag != Integer.parseInt(Is[1]))
			// {
			// SupplierInquiry NewSupplierInquiry = new SupplierInquiry();
			// NewSupplierInquiry.setSign(SupplierInquiryObject.getSign());
			// NewSupplierInquiry.setFlowstatus(SupplierInquiryObject
			// .getFlowstatus());
			// NewSupplierInquiry.setRemark(SupplierInquiryObject
			// .getRemark());
			// NewSupplierInquiry.setRecorder(SupplierInquiryObject
			// .getRecorder());
			// NewSupplierInquiry.setProcess(SupplierInquiryObject
			// .getProcess());
			// NewSupplierInquiry.setPurchase(SupplierInquiryObject
			// .getPurchase());
			// NewSupplierInquiry.setProcessTag(2);
			// NewSupplierInquiry.setOrderTag(0);
			// NewSupplierInquiry.setSupplier(supplier);
			// this.getSupplierInquiryService().save(NewSupplierInquiry);
			//						
			// Inquiry InquiryObject = this.getInquiryService()
			// .getInquiry(Integer.parseInt(Is[0]));
			// InquiryObject.setProcessTag(2);
			// InquiryObject.setSupplierInquiry(NewSupplierInquiry);
			// this.getInquiryService().saveOrUpdate(InquiryObject);
			// } else {
			// Inquiry InquiryObject = this.getInquiryService()
			// .getInquiry(Integer.parseInt(Is[0]));
			// InquiryObject.setProcessTag(2);
			// this.getInquiryService().saveOrUpdate(InquiryObject);
			// SupplierInquiryObject.setSupplier(supplier);
			// this.getSupplierInquiryService().saveOrUapedate(
			// SupplierInquiryObject);
			// }
			// supplierJudgeTag = Integer.parseInt(Is[1]);
			// judgeTag++;
			//						
			// }

		} else {
			this.addActionError("供应商选择有误!重新进行处理");
			return ERROR;
		}

		return SUCCESS;
	}

	@Action("selectSupplierInquiry")
	public String selectSupplierInquiry() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Supplier.class);
		this.setPagination(this.getSupplierService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("editInquiry")
	public String editInquiry() {

		/**
		 * 根据ID找到供应商
		 */
		suppliers = this.getSupplierService().findByIds(ids);
		return SUCCESS;
	}

	@Action("listPassSupplierInquiry")
	public String listPassSupplierInquiry() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				SupplierInquiry.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getSupplierInquiryService().pageQueryValid(
				criteria, pagination));
		return SUCCESS;
	}

	@Action("listMyPassSupplierInquiry")
	public String listMyPassSupplierInquiry() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				SupplierInquiry.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		Person person = AppContext.getAccountContext().getUser();
		criteria.add(Property.forName("recorder").eq(person));
//		criteria.add(Property.forName("flowstatus").eq(
//				Constrains.WORKFLOWENTITY_STATE_VALID));
		this.setPagination(this.getSupplierService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listSupplierInquiryProcess")
	public String listSupplierInquiryProcess() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				SupplierInquiry.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		Person person = AppContext.getAccountContext().getUser();
		criteria.add(Property.forName("recorder").eq(person));
		criteria.add(Property.forName("flowstatus").eq(
				Constrains.WORKFLOWENTITY_STATE_VALID));
		// criteria.add(Property.forName("processTag").eq(
		// Constrains.PURCHASE_PROCESS_A));
		this.setPagination(this.getSupplierService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listPurchasetoOrder")
	public String listPurchasetoOrder() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				SupplierInquiry.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		Person person = AppContext.getAccountContext().getUser();
		criteria.add(Property.forName("recorder").eq(person));
		criteria.add(Property.forName("flowstatus").eq(
				Constrains.WORKFLOWENTITY_STATE_VALID));
		criteria.add(Property.forName("processTag").eq(
				Constrains.PURCHASE_PROCESS_B));
		this.setPagination(this.getSupplierService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("viewSupplierInquiry")
	public String viewSupplierInquiry() {
		this.displayEndNodes(supplierInquiry);
		return SUCCESS;
	}

	@Action("PurchaseOrderProcess")
	public String PurchaseOrderProcess() {
		return SUCCESS;
	}

	public SupplierInquiryService getSupplierInquiryService() {
		return supplierInquiryService;
	}

	public void setSupplierInquiryService(
			SupplierInquiryService supplierInquiryService) {
		this.supplierInquiryService = supplierInquiryService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public PurchaseService getPurchaseService() {
		return purchaseService;
	}

	public void setPurchaseService(PurchaseService purchaseService) {
		this.purchaseService = purchaseService;
	}

	public Purchase getPurchase() {
		return purchase;
	}

	public void setPurchase(Purchase purchase) {
		this.purchase = purchase;
	}

	public Collection<PurchaseDetail> getSlist() {
		return slist;
	}

	public void setSlist(Collection<PurchaseDetail> slist) {
		this.slist = slist;
	}

	public String getJoiners() {
		return joiners;
	}

	public void setJoiners(String joiners) {
		this.joiners = joiners;
	}

	public SupplierService getSupplierService() {
		return supplierService;
	}

	public void setSupplierService(SupplierService supplierService) {
		this.supplierService = supplierService;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}

	public Supplier getSupplier() {
		return supplier;
	}

	public void setSupplier(Supplier supplier) {
		this.supplier = supplier;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public Collection<Supplier> getSuppliers() {
		return suppliers;
	}

	public void setSuppliers(Collection<Supplier> suppliers) {
		this.suppliers = suppliers;
	}

	public String getStockNum() {
		return stockNum;
	}

	public void setStockNum(String stockNum) {
		this.stockNum = stockNum;
	}

	public PurchaseDetail getPurchaseDetail() {
		return purchaseDetail;
	}

	public void setPurchaseDetail(PurchaseDetail purchaseDetail) {
		this.purchaseDetail = purchaseDetail;
	}

	public SupplierInquiry getSupplierInquiry() {
		return supplierInquiry;
	}

	public void setSupplierInquiry(SupplierInquiry supplierInquiry) {
		this.supplierInquiry = supplierInquiry;
	}

	public List<Inquiry> getInquirys() {
		return inquirys;
	}

	public void setInquirys(List<Inquiry> inquirys) {
		this.inquirys = inquirys;
	}

	public PurchaseDetail[] getPurchasedetail() {
		return purchasedetail;
	}

	public void setPurchasedetail(PurchaseDetail[] purchasedetail) {
		this.purchasedetail = purchasedetail;
	}

	public Double[] getNPrice() {
		return nPrice;
	}

	public void setNPrice(Double[] price) {
		nPrice = price;
	}

	public Double[] getNDiscount() {
		return nDiscount;
	}

	public void setNDiscount(Double[] discount) {
		nDiscount = discount;
	}

	public Double[] getNTotalPrice() {
		return nTotalPrice;
	}

	public void setNTotalPrice(Double[] totalPrice) {
		nTotalPrice = totalPrice;
	}

	public PurchaseDetail[] getSupplierids() {
		return supplierids;
	}

	public void setSupplierids(PurchaseDetail[] supplierids) {
		this.supplierids = supplierids;
	}

	public Inquiry[] getSupplierifors() {
		return supplierifors;
	}

	public void setSupplierifors(Inquiry[] supplierifors) {
		this.supplierifors = supplierifors;
	}

	public String[] getChecks() {
		return checks;
	}

	public void setChecks(String[] checks) {
		this.checks = checks;
	}

	public String[] getCheckss() {
		return checkss;
	}

	public void setCheckss(String[] checkss) {
		this.checkss = checkss;
	}

	public List<PurchaseType> getPurchaseTypeList() {
		return purchaseTypeList;
	}

	public void setPurchaseTypeList(List<PurchaseType> purchaseTypeList) {
		this.purchaseTypeList = purchaseTypeList;
	}

	public InquiryService getInquiryService() {
		return inquiryService;
	}

	public void setInquiryService(InquiryService inquiryService) {
		this.inquiryService = inquiryService;
	}

	public PurchaseTypeService getPurchaseTypeService() {
		return purchaseTypeService;
	}

	public void setPurchaseTypeService(PurchaseTypeService purchaseTypeService) {
		this.purchaseTypeService = purchaseTypeService;
	}

	public Inquiry getInquiry() {
		return inquiry;
	}

	public void setInquiry(Inquiry inquiry) {
		this.inquiry = inquiry;
	}

	public PurchaseOrder getPurchaseOrder() {
		return purchaseOrder;
	}

	public void setPurchaseOrder(PurchaseOrder purchaseOrder) {
		this.purchaseOrder = purchaseOrder;
	}

	public PurchaseOrderService getPurchaseOrderService() {
		return purchaseOrderService;
	}

	public void setPurchaseOrderService(
			PurchaseOrderService purchaseOrderService) {
		this.purchaseOrderService = purchaseOrderService;
	}

	public PurchaseDetailService getPurchaseDetailService() {
		return purchaseDetailService;
	}

	public void setPurchaseDetailService(
			PurchaseDetailService purchaseDetailService) {
		this.purchaseDetailService = purchaseDetailService;
	}

	public String getFlow() {
		return flow;
	}

	public void setFlow(String flow) {
		this.flow = flow;
	}

}
